package com.bdy.lm.taximanager.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by Administrator on 2016/8/3.
 */
public class JsonUtils {

    public static Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
    public static JSONObject result;

    /**
     * 将Map<String, Object>转化为String
     * @param messageMap
     * @return
     */
    public static String toGson(Map<String, Object> messageMap) {
        //result = gson.mapToJsonObject(messageMap);
        return gson.toJson(messageMap);
    }

    /**
     * 将Map<String, Object>转化为JSONObject 若map中包含有list则会转化失败
     * @param messageMap
     * @return
     */
    public static JSONObject mapToJsonObject(Map<String, Object> messageMap) {
        return new JSONObject(messageMap);
    }

    /**
     * 将Map<String, byte[]>转化为JSONObject
     * @param messageMap
     * @return
     */
    public static JSONObject byteToJson(Map<String, byte[]> messageMap) {
        return new JSONObject(messageMap);
    }

    /**
     * 将String转化为JSONObject
     * @param js
     * @return
     */
    public static JSONObject stringToJsonObject(String js) throws JSONException{
        return new JSONObject(js);
    }

    //public static
}
